import string

from flask import Blueprint, render_template, jsonify
from exts import mail, db
from flask_mail import Message

from flask import request

# 随机
import string
import random

from models import EmailCodeModel

bp = Blueprint = Blueprint('auth', __name__, url_prefix='/')


@bp.route('/login')
def login():
    pass


@bp.route('/register')
def register():
    return render_template("register.html")



@bp.route('/mail/test')
def test_mail():
    message = Message("测试邮件", recipients=["3239295856@qq.com"], body="这是一封测试邮件", sender="秋雨祎 <2394713824@qq.com>")
    mail.send(message)
    return "发送成功"

@bp.route('/captcha/email')
def captcha_email():
    email = request.args.get("email")
    # 生成随机验证码
    source = string.digits*4
    captcha = "".join(random.sample(source, 4))
    # 发送邮件
    message = Message("秋雨祎发送的验证码", recipients=["3239295856@qq.com"], body=f"你的验证码是{captcha}", sender="秋雨祎 <2394713824@qq.com>")
    mail.send(message)
    # 存储验证码，缓存
    email_captcha = EmailCodeModel(email=email, captcha=captcha)
    db.session.add(email_captcha)
    db.session.commit()

    # ERSTful API
    return jsonify({"code": 200, "message": "", "data": None})


